public class 寻找峰值 {
    public int findPeakElement(int[] nums) {
        int n = nums.length;
        if (n == 1) {
            return 0;
        }
        if (nums[0] > nums[1]) {
            return 0;
        }
        if (nums[n - 1] > nums[n - 2]) {
            return n - 1;
        }
        int l = 1;
        int r = n - 2;
        int ans = -1;
        while (l <= r) {
            int m = l + (r - l) / 2;
            if (nums[m - 1] > nums[m]) {
                r = m - 1;
            } else if (nums[m + 1] > nums[m]) {
                l = m + 1;
            } else {
                ans = m;
                break;
            }
        }
        return ans;
    }
}
